package com.ksd.pug.clound.controller;

import com.ksd.pug.clound.pojo.SysUser;
import com.ksd.pug.clound.service.UserRemoteService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/consumer")
public class UserController {

    @Autowired
    private UserRemoteService userRemoteService;

    @GetMapping("/user/{id}")
    @HystrixCommand(fallbackMethod = "fallbackUser", commandProperties = {
            // 如果请求并发数达到该设置的值，请求会被拒绝和抛出异常并且fallback不会被调用 默认是：10秒
            @HystrixProperty(name = HystrixPropertiesManager.FALLBACK_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, value = "15")
    })
    public SysUser cacheGetUser(@PathVariable("id") Long id) {
        return userRemoteService.findUser(id);
    }

    @GetMapping("/del/{id}")
    public void delCache(@PathVariable("id") Long id) {
        userRemoteService.delUser(id);
    }

    /**
     * 细节1：方法返回值必须一模一样，
     * 细节2：参数也必须一模一样
     */
    public SysUser fallbackUser(Long id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(-100L);
        return sysUser;
    }
}
